package com.shxc.smarttrade.community.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.base.BaseFragment;
import com.shxc.smarttrade.community.adapter.AndroidCommunityNewsAdapter;
import com.shxc.smarttrade.community.model.CommunityNewsModel;
import com.shxc.smarttrade.currency.activity.WebActivity;
import com.shxc.smarttrade.info.Contents;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisModel;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisUtils;
import com.shxc.smarttrade.utils.Loger;
import com.shxc.smarttrade.utils.MyOkHttpUtils;
import com.shxc.smarttrade.utils.ToastUtils;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.Event;
import org.xchris.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 社区 - 市场
 */
@ContentView(R.layout.fragment_swipe)
public class AndroidMarketFragment extends BaseFragment {

    @ViewInject(R.id.load_view)
    LinearLayout load_view;
    @ViewInject(R.id.loading_view)
    LinearLayout loading_view;
    @ViewInject(R.id.loadingfaild_view)
    LinearLayout loadingfaild_view;
    @ViewInject(R.id.swiperefreshlayout)
    SwipeRefreshLayout swiperefreshlayout;
    @ViewInject(R.id.recyclerview)
    RecyclerView recyclerview;
    @ViewInject(R.id.foot_view)
    View foot_view;

    private List<CommunityNewsModel.DataBean> list;
    private AndroidCommunityNewsAdapter adapter;


    private int page = 1;
    private int size = 5;
    private int cla = 2;

    private boolean isRefresh = true;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        loading_view.setVisibility(View.VISIBLE);
        list = new ArrayList<>();
        adapter = new AndroidCommunityNewsAdapter(mContext, list, handler);
        recyclerview.setAdapter(adapter);
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        //设置滑动监听事件
        swiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                isRefresh = true;
                initData();
            }
        });
        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (isRefresh) {
                    page ++;
                    foot_view.setVisibility(View.VISIBLE);
                    initData();
                }else{
                    ToastUtils.showToast("暂无更多数据");
                }
            }
        });

        initData();
    }

    @Event({R.id.loadingfaild_view})
    private void onClick(View view) {
        initData();
    }

    Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    //點擊跳轉

                    CommunityNewsModel.DataBean model = (CommunityNewsModel.DataBean) msg.obj;
                    bundle.putString("title", "新闻详情");
                    bundle.putString("url", Contents.H5PLUS + model.getId());
                    JumpTo(WebActivity.class, bundle);
//                    MobclickAgent.onEvent(getContext(), "transaction_detail");//友盟统计该点击事件发生次数
                    break;
            }
            super.dispatchMessage(msg);
        }
    };

    private void initData() {
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("size", size);
        map.put("cla", cla);

        MyOkHttpUtils.getServiceDatas(map,Contents.ARTICLE_SHOWMSG, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void success(String response, int d) {

                if (response != null) {
                    JsonAnalysisModel jam = JsonAnalysisUtils.jsonAnalysis(response, CommunityNewsModel.class);
                    if (jam.isSuccess()) {
                        CommunityNewsModel model = (CommunityNewsModel) jam.getData();
                        if (model.getErr_code().equals("0") && model.getData().size() > 0) {
                            if (page == 1) {
                                list.clear();
                                list = model.getData();
                            } else {
                                list.addAll(model.getData());
                            }
                            adapter.upDate(list);
                            load_view.setVisibility(View.GONE);
                            if (model.getData().size() < size) {
                                isRefresh = false;
                            }
                        } else {
                            if (page == 1 && model.getData().size() == 0) {
                                load_view.setVisibility(View.VISIBLE);
                            } else {
                                ToastUtils.showToast("暂无更多数据。");
                                load_view.setVisibility(View.GONE);
                                loadingfaild_view.setVisibility(View.GONE);
                            }
                        }
                        foot_view.setVisibility(View.GONE);
                    } else {
                        ToastUtils.showToast(jam.getMsg());
                    }
                }
                swiperefreshlayout.setRefreshing(false);
                loadingDialog.dismiss();
            }

            @Override
            public void faild(Call call, Exception e, int id) {
                swiperefreshlayout.setRefreshing(false);
                load_view.setVisibility(View.VISIBLE);
                loadingfaild_view.setVisibility(View.VISIBLE);
                foot_view.setVisibility(View.GONE);
                loadingDialog.dismiss();
            }
        });
    }
}
